﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace ZhimeiSoft.CharmManager.Client.Forms.Utils
{
    public class ToolAppConfig
    {
        private string configFileName = string.Empty;

        private XmlDocument xd = new XmlDocument();

        private List<ToolEntity> tools = new List<ToolEntity>();

        public string ConfigFileName
        {
            get
            {
                string result = string.Empty;
                if (string.IsNullOrEmpty(this.configFileName))
                    result = "CharmConfig.xml";
                else
                    result = this.configFileName;
                return result;
            }
        }

        public XmlDocument XmlDocument
        {
            get { return this.xd; }
            set { this.xd = value; }
        }

        public List<ToolEntity> Tools
        {
            get { return this.tools; }
            set { this.tools = value; }
        }

        public ToolAppConfig(string fileName)
        {
            if (!File.Exists(fileName))
            {
                throw new FileNotFoundException("找不到文件", fileName);
            }
            this.configFileName = fileName;
            this.Init();
        }

        private void Init()
        {
            this.xd.Load(this.configFileName);
            this.LoadConfig(xd);
        }

        public void LoadConfig(XmlDocument xml)
        {
            //具体的加载逻辑
            XmlNode node = xml.SelectSingleNode("config/tools");
            if (node != null && node.ChildNodes.Count > 0)
            {
                foreach (XmlNode currentNode in node.ChildNodes)
                {
                    if (currentNode != null && string.Compare(currentNode.Name, "tool", true) == 0)
                    {
                        ToolEntity tool = new ToolEntity();
                        //tool.Name = currentNode.Attributes["name"].ToString();
                        tool.Image = currentNode.Attributes["image"].Value.ToString();
                        tool.Title = currentNode.Attributes["title"].Value.ToString();
                        tool.Url = currentNode.Attributes["type"].Value.ToString();
                        this.tools.Add(tool);
                    }
                }
            }
        }
    }
}
